const questionsData = {
	'math': [
		{
			type: 'single',
			question: "1 + 1等于多少？",
			options: { A: "1", B: "2", C: "3" },
			answer: "B",
			explanation: "基本数学加法"
		},
		{
			type: 'multiple',
			question: "以下哪些是质数？",
			options: { A: "2", B: "4", C: "5", D: "7" },
			answer: ["A", "C", "D"],
			explanation: "质数是指只能被1和它本身整除的数"
		}
	],
	'history': [
		{
			type: 'single',
			question: "中国第一个朝代是？",
			options: { A: "夏朝", B: "商朝", C: "周朝" },
			answer: "A",
			explanation: "根据历史记载，夏朝是中国第一个世袭制朝代"
		}
	]
};

let totalAnswered = 0;
let correctCount = 0;

function loadQuestions(category) {
	const container = document.getElementById('questions-container');
	container.innerHTML = '';
	let list = [];
	if(category == null) {
		for(key in questionsData){
			list = list.concat(questionsData[key]);
		}
		console.log(list);
	}else{
		list = questionsData[category];
	}
	list.forEach((q, index) => {
		const questionDiv = document.createElement('div');
		questionDiv.className = 'question-card';
		questionDiv.innerHTML = `
                   <h3>题目 ${index + 1}: ${q.question}</h3>
                   <div class="options">
                       ${Object.entries(q.options).map(([key, value]) => `
                           <label>
                               <input type="${q.type === 'single' ? 'radio' : 'checkbox'}" 
                                     name="question${index}" 
                                     value="${key}">
                               ${key}. ${value}
                           </label>
                       `).join('')}
                   </div>
                   ${q.type === 'multiple' ? '<button onclick="checkMultiple(this, ' + index + ')">确定</button>' : ''}
                   <div class="answer-info"></div>
               `;

		if (q.type === 'single') {
			questionDiv.querySelectorAll('input').forEach(input => {
				input.addEventListener('change', () => checkSingle(input, q, questionDiv));
			});
		}

		container.appendChild(questionDiv);
	});
}

function checkSingle(input, question, container) {
	const answerDiv = container.querySelector('.answer-info');
	const isCorrect = input.value === question.answer;

	container.querySelectorAll('label').forEach(label => {
		label.classList.remove('correct', 'incorrect');
		const radio = label.querySelector('input');
		if (radio.checked) {
			label.classList.add(isCorrect ? 'correct' : 'incorrect');
		}
		if (radio.value === question.answer) {
			label.style.border = '2px solid #4CAF50';
		}
	});

	updateStatistics(isCorrect);
	showAnswerInfo(isCorrect, question, answerDiv);
}

function checkMultiple(button, index) {
	const container = button.parentElement;
	const answerDiv = container.querySelector('.answer-info');
	const checkboxes = container.querySelectorAll('input[type="checkbox"]');
	const selected = Array.from(checkboxes)
		.filter(cb => cb.checked)
		.map(cb => cb.value);

	const isCorrect = JSON.stringify(selected.sort()) === JSON.stringify(questionsData.math[index].answer.sort());

	checkboxes.forEach(cb => {
		const label = cb.parentElement;
		label.classList.remove('correct', 'incorrect');
		if (cb.checked) {
			label.classList.add(isCorrect ? 'correct' : 'incorrect');
		}
		if (questionsData.math[index].answer.includes(cb.value)) {
			label.style.border = '2px solid #4CAF50';
		}
	});

	updateStatistics(isCorrect);
	showAnswerInfo(isCorrect, questionsData.math[index], answerDiv);
}

function updateStatistics(isCorrect) {
	totalAnswered++;
	if (isCorrect) correctCount++;

	document.getElementById('total-answered').textContent = totalAnswered;
	document.getElementById('correct-count').textContent = correctCount;
	document.getElementById('accuracy').textContent = (correctCount / totalAnswered * 100 || 0).toFixed(2);
}

function showAnswerInfo(isCorrect, question, container) {
	if (!isCorrect) {
		container.innerHTML = `
                   <div class="correct-answer">正确答案：${Array.isArray(question.answer) ? question.answer.join(', ') : question.answer}</div>
                   <div class="explanation">解析：${question.explanation}</div>
               `;
	} else {
		container.innerHTML = '';
	}
}

function loadType(){
	$.post('/getType', function(data){
		
	})
}

// 默认加载数学题
loadQuestions(null);